﻿Public Class clsDataStruc
    Public Function CreateDataSet() As DataSet
        Try
            Dim ds As New DataSet
            Dim dt As New DataTable("Raw")
            Dim collist As String() = New String() {COL_TASK, COL_KEY, COL_DUR, COL_PRE, COL_POST}
            For Each co As String In collist
                Dim col As New DataColumn(co)
                dt.Columns.Add(col)
            Next
            ds.Tables.Add(dt)
            Return ds
        Catch ex As Exception
            Throw
        End Try
    End Function
    Public Function FillDemoData(ByRef dt As DataTable) As Boolean
        Try
            'minized test set
            Dim o As New task
            Dim dtt As task.rawDataTable = o.raw
            Dim row As task.rawRow = dtt.NewrawRow()
            row.key = 1
            row.dur = 0
            row.renewableRes = "R1@0;R2@0;R3@0;R4@0"
            row.pre = ""
            row.post = "2,3"
            row.task = "task1"
            dtt.AcceptChanges()
            row = dtt.NewrawRow()
            row.key = 2
            row.dur = 4
            row.renewableRes = "R1@1;R2@0;R3@4;R4@1"
            row.pre = "1"
            row.post = "4"
            row.task = "task2"
            dtt.AcceptChanges()
            row = dtt.NewrawRow()
            row.key = 3
            row.dur = 3
            row.renewableRes = "R1@2;R2@0;R3@4;R4@1"
            row.pre = "1"
            row.post = "4"
            row.task = "task3"
            dtt.AcceptChanges()
            row = dtt.NewrawRow()
            row.key = 4
            row.dur = 0
            row.renewableRes = "R1@0;R2@0;R3@0;R4@0"
            row.pre = "3,4"
            row.post = ""
            row.task = "task4"
            dtt.AcceptChanges()
            dt = dtt
            Return True
        Catch ex As Exception
            Throw
        End Try
    End Function
    Public Function DataToObject(ByVal data As DataSet) As Dictionary(Of Int32, clsTask)
        Try
            Dim ht As New Dictionary(Of Int32, clsTask)
            If Not data Is Nothing Then
                If data.Tables.Contains("Raw") Then
                    For Each dr As task.rawRow In data.Tables("Raw").Rows
                        Dim res As New Dictionary(Of String, Int32)
                        Dim resv() As String = dr.renewableRes.Split(";")
                        For Each Val As String In resv
                            Dim pair() As String = Val.Split("@")
                            res.Add(pair(0), CLng(pair(1)))
                        Next
                        Dim pre As HashSet(Of Int32) = GetPostPreCol(dr(COL_PRE).ToString)
                       
                        Dim task As clsTask = New clsTask With {.Key = CLng(dr(COL_KEY)),
                                                              .Duration = CLng(dr(COL_DUR)),
                                                              .Name = dr(COL_TASK).ToString,
                                                              .Post = GetPostPreCol(dr(COL_POST).ToString),
                                                              .Pre = pre,
                                                                .RenewableResources = res}

                        ht.Add(task.Key, task)
                    Next
                End If
            End If

            For Each task As clsTask In ht.Values
                task.AggPre = New HashSet(Of Int32)
                Dim pre As HashSet(Of Int32) = task.Pre
                For Each p In pre
                    If p > 0 Then
                        Dim preTask As clsTask = ht(p)
                        For Each pp As Int32 In preTask.AggPre
                            If pp > 0 Then
                                If Not task.AggPre.Contains(pp) Then
                                    task.AggPre.Add(pp)
                                End If
                            End If
                        Next
                        If Not task.AggPre.Contains(p) Then
                            task.AggPre.Add(p)
                        End If
                    End If
                Next

            Next

            Return ht
        Catch ex As Exception
            Throw
        End Try
    End Function

    Private Function GetPostPreCol(info As String) As HashSet(Of Int32)
        Try
            Dim list As New HashSet(Of Int32)
            For Each itm As String In info.Split(",")
                If info = "" Then
                    itm = 0
                End If
                list.Add(itm)
            Next
            Return list
        Catch ex As Exception
            Throw
        End Try
    End Function
End Class
